package com.hhf.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhf.job.entity.BaseJob;
import com.hhf.job.mapper.BaseJobMapper;
import com.hhf.job.service.BaseJobService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BaseJobServiceImpl extends ServiceImpl<BaseJobMapper, BaseJob> implements BaseJobService {
    @Override
    public List<BaseJob> getJob() {
        QueryWrapper<BaseJob> qw = new QueryWrapper();
        qw.eq("state",1);
        return list(qw);
    }

    @Override
    public List<BaseJob> getAllJob(BaseJob job) {
        QueryWrapper qw = new QueryWrapper();
        Integer jobId = job.getJobId();
        String jobName = job.getJobName();
        Integer state = job.getState();
        qw.eq(jobId != null,"JOB_ID", jobId);
        qw.eq(StringUtils.isNotBlank(jobName),"JOB_NAME", jobName);
        qw.eq(state != null,"STATE", state);
        return list(qw);
    }

    @Override
    public BaseJob getJobById(Integer jobId) {
        return getById(jobId);
    }


    @Override
    public boolean updateJobExecStatus(Integer jobId, Integer from, Integer to) {
        UpdateWrapper<BaseJob> uw = new UpdateWrapper<>();
        uw.eq(jobId != null, "job_id", jobId);
        uw.eq(from != null, "exec_status", from);
        uw.set("exec_status",to);
        return update(uw);
    }

    @Override
    public boolean updateNextExecTime(Integer jobId, Date date) {
        UpdateWrapper<BaseJob> uw = new UpdateWrapper<>();
        uw.eq("job_id", jobId);
        uw.set("next_exec_time", date);
        return update(uw);
    }
}
